package cn.iocoder.yudao.module.wms.controller.admin.pomain;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.pomain.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.pomain.PomainDO;
import cn.iocoder.yudao.module.wms.dal.dataobject.pomain.PomainsDO;
import cn.iocoder.yudao.module.wms.service.pomain.PomainService;

@Tag(name = "管理后台 - 采购订单主")
@RestController
@RequestMapping("/wms/pomain")
@Validated
public class PomainController {

    @Resource
    private PomainService pomainService;

    @PostMapping("/create")
    @Operation(summary = "创建采购订单主")
    @PreAuthorize("@ss.hasPermission('wms:pomain:create')")
    public CommonResult<Integer> createPomain(@Valid @RequestBody PomainSaveReqVO createReqVO) {
        return success(pomainService.createPomain(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新采购订单主")
    @PreAuthorize("@ss.hasPermission('wms:pomain:update')")
    public CommonResult<Boolean> updatePomain(@Valid @RequestBody PomainSaveReqVO updateReqVO) {
        pomainService.updatePomain(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除采购订单主")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:pomain:delete')")
    public CommonResult<Boolean> deletePomain(@RequestParam("id") Integer id) {
        pomainService.deletePomain(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得采购订单主")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:pomain:query')")
    public CommonResult<PomainRespVO> getPomain(@RequestParam("id") String id) {
        try {
            PomainDO  pomain =pomainService.getPomain(Integer.valueOf(id));
            return success(BeanUtils.toBean(pomain, PomainRespVO.class));

        }catch (Exception e)
        {
            PomainDO  pomain=  pomainService.findByProcessInstanceId(id);
            return success(BeanUtils.toBean(pomain, PomainRespVO.class));
        }
    }

    @GetMapping("/page")
    @Operation(summary = "获得采购订单主分页")
    @PreAuthorize("@ss.hasPermission('wms:pomain:query')")
    public CommonResult<PageResult<PomainRespVO>> getPomainPage(@Valid PomainPageReqVO pageReqVO) {
        PageResult<PomainDO> pageResult = pomainService.getPomainPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PomainRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "按条件获得采购订单")
    @PreAuthorize("@ss.hasPermission('wms:pomain:query')")
    public CommonResult<List<PomainRespVO>> list(@Valid PomainPageReqVO pageReqVO) {
        List<PomainRespVO> pageResult = pomainService.list(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出采购订单主 Excel")
    @PreAuthorize("@ss.hasPermission('wms:pomain:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPomainExcel(@Valid PomainPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PomainDO> list = pomainService.getPomainPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "采购订单主.xls", "数据", PomainRespVO.class,
                        BeanUtils.toBean(list, PomainRespVO.class));
    }

    @GetMapping("/startProcess")
    @Operation(summary = "启动流程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:production-order:create')")
    public CommonResult<Boolean> startProcess(@RequestParam("id") Integer id) {
        pomainService.process(id);
        return success(true);
    }


    // ==================== 子表（采购订单子） ====================

    @GetMapping("/pomains/list-by-pid")
    @Operation(summary = "获得采购订单子列表")
    @Parameter(name = "pid", description = "主表ID")
    @PreAuthorize("@ss.hasPermission('wms:pomain:query')")
    public CommonResult<List<PomainsRespVO>> getPomainsListByPid(@RequestParam("pid") Integer pid) {
        return success(pomainService.findPomainsByPid(pid));
    }


    @GetMapping("/getPomainsById")
    @Operation(summary = "获得采购订单子表详细信息")
    @Parameter(name = "id", description = "ID")
    @PreAuthorize("@ss.hasPermission('wms:pomain:query')")
    public CommonResult<PomainsRespVO> getPomainsById(@RequestParam("id") Integer id) {
        return success(pomainService.findPomainsById(id));
    }

    @GetMapping("/pomains/operateList")
    @Operation(summary = "获得采购订单子列表 用于入库等操作")
    @Parameter(name = "pid", description = "主表ID")
    @PreAuthorize("@ss.hasPermission('wms:pomain:query')")
    public CommonResult<List<PomainsRespVO>> operateList(@RequestParam("pid") Integer pid) {
        return success(pomainService.findPomainsOperateList(pid));
    }

    @GetMapping("/pomains/getPomainsOperateListByParams")
    @Operation(summary = "获得采购订单子列表 用于入库等操作")
    @Parameter(name = "pid", description = "主表ID")
    @PreAuthorize("@ss.hasPermission('wms:pomain:query')")
    public CommonResult<List<PomainsRespVO>> getPomainsOperateListByParams(@Valid PomainsRespVO pomainsRespVO) {
        return success(pomainService.getPomainsOperateListByParams(pomainsRespVO));
    }

}